package org.layui.servlet;

import org.layui.dao.UserDao;
import org.layui.dao.impl.UserDaoImpl;
import org.layui.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/user/list")
public class UserListServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取当前页和每页显示的记录数
        Integer page = Integer.parseInt(req.getParameter("page"));
        Integer limit = Integer.parseInt(req.getParameter("limit"));

        // 获取查询的条件
        String username = req.getParameter("username");
        String level = req.getParameter("level");

        // 把查询条件封装到User对象中
        User user = new User();
        user.setUsername(username);
        user.setLevel(level);

        // 创建DAO对象，实现数据的查询（多条件+分页）
        UserDao userDao = new UserDaoImpl();
        List<User> list = userDao.queryUserByPage(user, page, limit);
        Long count = userDao.queryUserByPageCount(user);

        // 把分页的信息，封装到PageVO对象中，并返回到客户端
        print(resp, pageVO(count, list));
    }
}
